package com.jun.flower.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jun.flower.bean.PageResult;
import com.jun.flower.bean.TbUser;
import com.jun.flower.bean.TbUserExample;
import com.jun.flower.bean.TbUserExample.Criteria;
import com.jun.flower.service.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping("/user/show")
	@ResponseBody
	public TbUser getUserById(Integer id) {
		TbUser user = userService.getUserById(id);
		return user;
	}
	
	@RequestMapping("/user/list")
	public String getUserList(Model model,@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) {
		PageResult<TbUser> pageResult = userService.getUserList(pageNum,pageSize);
		model.addAttribute("userList", pageResult.getList());
		model.addAttribute("page", pageResult);
		return "user-list";
	}
	
	@RequestMapping("/user/check/username")
	@ResponseBody
	public Map checkUserName(String username) {
		boolean result = userService.checkUsername(username);
		Map data = new HashMap<>();
		data.put("status", result);
		return data;
	}
	//后台保存用户
	@RequestMapping("/user/save")
	public String saveUser(TbUser user) {
		//把用户数据添加到数据库中
		userService.addUser(user);
		//跳转到用户列表页面
		return "redirect:/user/list";
	}
	
	//前台保存用户
	@RequestMapping("/user/add")
	public String addUser(Model model,TbUser user) {
		//把用户数据添加到数据库中
		userService.addUser(user);
		TbUser user2 = userService.getUserByUserName(user.getUsername());
		model.addAttribute("user", user2);
		return "forward:/web/index";
	}
	
	@RequestMapping("/user/delete")
	@ResponseBody
	public Map deleteUser(String ids) {
		Boolean result = userService.deleteUser(ids);
		Map data = new HashMap<>();
		data.put("state", result);
		return data;
	}
	
	@RequestMapping("/user/update")
	public String updateUser(Model model,Integer id) {
		TbUser user = userService.getUserById(id);
		model.addAttribute("user", user);
		return "user-update";
	}
	
	@RequestMapping("/user/updateById")
	public String updateUserById(TbUser user) {
		userService.updateUserById(user);
		return "redirect:/user/list";
	}
	
	@RequestMapping("/user/login/username")
	@ResponseBody
	public Map loginUser(Model model,String username,String password) {
		TbUser user = userService.loginUser(username,password);
		Map data = new HashMap<>();
		data.put("user", user);
		return data;
	}
	
	@RequestMapping("/user/login")
	public String loginUser(Model model,TbUser user) {
		TbUser user2 = userService.getUserByUserName(user.getUsername());
		model.addAttribute("user", user2);
		return "forward:/web/index";
	}
	
	@RequestMapping("/car/login")
	public String loginCar(Model model,TbUser user,Integer Id) {
		TbUser user2 = userService.getUserByUserName(user.getUsername());
		model.addAttribute("user", user2);
		return "forward:/web/flower?id="+Id;
	}
	
}
